package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.EmployeesModel;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Krzysztof
 */
@Stateful
public class EmployeeDAOImpl implements EmployeeDAO {

    private EntityManager entityManager;

    public EmployeeDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public EmployeesModel getEmployeeById(Integer id) {
        return (EmployeesModel) entityManager.createNamedQuery("EmployeesModel.findByEmployeeId").setParameter("employeeId", id).getSingleResult();
    }
    
    @Override
    public List<EmployeesModel> getAllEmployees() {
        return (List<EmployeesModel>) entityManager.createNamedQuery("EmployeesModel.findAll").getResultList();
    }

    @Override
    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    @Override
    public void addEmployee(EmployeesModel em) {
        entityManager.getTransaction().begin();
        entityManager.persist(em);
            entityManager.flush();
        entityManager.refresh(em);
        entityManager.getTransaction().commit();
    }

    @Override
    public int getEmployeesModelSize() {
        return entityManager.createNamedQuery("EmployeesModel.findAll").getResultList().size();
    }

    @Override
    public void removeEmployeeById(Integer id) {
        EmployeesModel rowToDelete = getEmployeeById(id);
        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();
    }


    @Override
    public int getEmployeesByEmploymentDateSize(Date employmentDate) {
        
     List<EmployeesModel> list = (List<EmployeesModel>) entityManager.createNamedQuery("EmployeesModel.findByEmploymentDate").setParameter("employmentDate", employmentDate).getResultList();
     return list.size();
    }

    @Override
    public void updateEmployee(EmployeesModel em) {
        try{
            EmployeesModel actual = getEmployeeById(em.getEmployeeId());
            entityManager.getTransaction().begin();
            entityManager.persist(actual);

            actual.setCity(em.getCity());
            actual.setComments(em.getComments());
            actual.setEmail(em.getEmail());
            actual.setEmploymentDate(em.getEmploymentDate());
            actual.setFirstname(em.getFirstname());
            actual.setHouseNumber(em.getHouseNumber());
            actual.setJobType(em.getJobType());
            actual.setLocalNumber(em.getLocalNumber());
            actual.setPassword(em.getPassword());
            actual.setPeselNumber(em.getPeselNumber());
            actual.setPhone(em.getPhone());
            actual.setReleaseDate(em.getReleaseDate());
            actual.setSalary(em.getSalary());
            actual.setStreet(em.getStreet());
            actual.setSurname(em.getSurname());
            actual.setZipCode(em.getZipCode());
            
            entityManager.getTransaction().commit();
        }
        catch(Throwable t){
            updateEmployee(em);
        }
    }

}
